package com.wpj.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.wpj.entity.Goods;
import com.wpj.service.ISearchService;
import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SearchServiceImpl implements ISearchService {

    @Autowired
    private SolrClient solrClient;

    @Override
    public List<Goods> searchGoodsListByKey(String key) {
        List<Goods> goodsList = new ArrayList<>();
        // 1.设置查询条件
        SolrQuery solrQuery = new SolrQuery();
        if (!StringUtils.isEmpty(key)) {
            solrQuery.setQuery(String.format("gname:%s || gdesc:%s", key, key));
            // 开启高亮
            solrQuery.setHighlight(true);

            // 设置高亮的字段
            solrQuery.addHighlightField("gname");

            // 设置高亮字段的字体颜色
            solrQuery.setHighlightSimplePre("<font color ='red'>");
            solrQuery.setHighlightSimplePost("</font>");
        } else {
            solrQuery.setQuery("gname:* || gdesc:*");
        }
        try {

            // 设置分页
            //  solrQuery.setStart(1);
            //  solrQuery.setRows(10);

            // 2.到索引库查询
            QueryResponse queryResponse = solrClient.query(solrQuery);

            // 3.得到结果(gname或者gdesc里面有匹配的结果)
            SolrDocumentList solrDocumentList = queryResponse.getResults();

            // 得到高亮的结果(gname)
            Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();

            for (SolrDocument document : solrDocumentList) {

                Goods goods = new Goods();
                String id = document.get("id").toString();
                goods.setId(Integer.parseInt(id));
                goods.setGname(document.get("gname").toString());
                goods.setGdesc(document.get("gdesc").toString());
                goods.setGpic(document.get("gpic").toString());
                Double gprice = Double.valueOf(document.get("gprice").toString());
                goods.setGprice(BigDecimal.valueOf(gprice));

                if (highlighting != null) {
                    // 先判断当前商品中是否存在高亮
                    Map<String, List<String>> map = highlighting.get(id);
                    if (map != null) {
                        // 覆盖掉前面没有高亮的字段
                        List<String> gnameList = map.get("gname");
                        if (gnameList != null) {
                            goods.setGname(gnameList.get(0));
                        }
                    }
                }
                goodsList.add(goods);
            }
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return goodsList;
    }

    @Override
    public void addGoods(Goods goods) {
        SolrInputDocument document = new SolrInputDocument();
        document.addField("id",goods.getId());
        document.setField("gname", goods.getGname());
        document.setField("gdesc", goods.getGdesc());
        document.setField("gpic", goods.getGpic());
        document.setField("gprice", goods.getGprice().toString());
        try {
            solrClient.add(document);
            solrClient.commit();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
